package com.ygxsk.carhome.views.picker;

import android.app.Dialog;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import androidx.fragment.app.DialogFragment;

import com.ygxsk.carhome.R;

/**
 * Autuhor:itchen
 * Time:2020/1/3 0003  17:53
 * Description: this is 弹窗的正常的年月日的时间滚轮内容
 */
public class YearPickerDialogFragment extends DialogFragment {

    protected YearPicker mYearPicker;

    //这里仅仅只有年信息
    private int mSelectedYear = -1;// mSelectedMonth = -1, mSelectedDay = -1;

    /**
     * 年份的选择器内容
     */
    private OnYearChooseListener mOnDateChooseListener;

    private boolean mIsShowAnimation = true;
    protected TextView mCancelButton, mDecideButton;

    public void setOnYearChooseListener(OnYearChooseListener onDateChooseListener) {
        mOnDateChooseListener = onDateChooseListener;
    }

    public void showAnimation(boolean show) {
        mIsShowAnimation = show;
    }

    public static YearPickerDialogFragment newInstance(int number) {

        Bundle args = new Bundle();

        YearPickerDialogFragment fragment = new YearPickerDialogFragment();
        // Supply num input as an argument.
        args.putInt("num", number);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.dialogyear, container);

        mYearPicker = (YearPicker) view.findViewById(R.id.yearpicker_dialog);

        //mYearPicker.setShowCurtainBorder(false);//不要粗体的
        //设置幕布间隔颜色
        //mYearPicker.setCurtainBorderColor(R.color.com_ycuwq_datepicker_divider);

        mCancelButton = (TextView) view.findViewById(R.id.btn_dialog_year_cancel);
        mDecideButton = (TextView) view.findViewById(R.id.btn_dialog_year_decide);

        mCancelButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });

        mDecideButton.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {

                if (mOnDateChooseListener != null) {

                    mOnDateChooseListener.onYearChoose(mYearPicker.getSelectedYear());

                }

                dismiss();
            }
        });

        if (mSelectedYear > 0) {
            setSelectedDate();
        }

        initChild();

        return view;
    }

    protected void initChild() {

    }

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {

        Dialog dialog = new Dialog(getActivity(), R.style.DatePickerBottomDialog);
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE); // 设置Content前设定

        dialog.setContentView(R.layout.dialogyear);
        dialog.setCanceledOnTouchOutside(true); // 外部点击取消

        Window window = dialog.getWindow();
        if (window != null) {
            if (mIsShowAnimation) {
                window.getAttributes().windowAnimations = R.style.DatePickerDialogAnim;
            }
            WindowManager.LayoutParams lp = window.getAttributes();
            lp.gravity = Gravity.BOTTOM; // 紧贴底部
            lp.width = WindowManager.LayoutParams.MATCH_PARENT; // 宽度持平
            lp.dimAmount = 0.35f;
            window.setAttributes(lp);
            window.addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
        }

        return dialog;
    }

    public void setSelectedDate(int year) {

        mSelectedYear = year;
        //没有月，日
        //mSelectedMonth = month;
        //mSelectedDay = day;
        setSelectedDate();
    }

    private void setSelectedDate() {
        if (mYearPicker != null) {
            mYearPicker.setSelectedYear(mSelectedYear);
        }
    }

    public interface OnYearChooseListener {
        void onYearChoose(int year);
    }

}
